package org.song.service.impl;

import org.song.exception.myException.CommonException;
import org.song.service.FileService;
import org.song.service.impl.common.AbstractBaseServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URL;

/**
 * 文件业务层
 */
@Service( value = "fileServiceImpl" )
public class FileServiceImpl extends AbstractBaseServiceImpl implements FileService {

    // 博客主图存放路径
    private static String BLOG_MAIN_IMAGE_PATH;

    // 博客内容图存放路径
    private static String BLOG_IMAGE_PATH;

    @Value( value = " ${oss.blog-main-image-path} " )
    public void setBlogMainImagePath(String blogMainImagePath) {
        BLOG_MAIN_IMAGE_PATH = blogMainImagePath;
    }

    @Value( value = " ${oss.blog-image-path} " )
    public void setBlogImagePath(String blogImagePath) {
        BLOG_IMAGE_PATH = blogImagePath;
    }

    /**
     * 上传博客主图
     * @param file
     * @return 保存OSS的对象名
     */
    @Override
    public String uploadBlogMainImage(MultipartFile file ) {
        try {
            return this.baseUploadImage( BLOG_MAIN_IMAGE_PATH , file );
        } catch (IOException e) {
            log.error( "文件解析错误" );
            throw new CommonException( "文件解析错误" );
        }
    }

    /**
     * 获取博客主图
     * @param objectName
     * @return
     */
    @Override
    public URL getBlogMainImageFromOssByObjectName( @NonNull String objectName , @Nullable Long expire) {
        return this.baseGetImage( objectName , expire);
    }

    /**
     * 上传博客内容图片
     * @param file
     * @return
     */
    @Override
    public String uploadBlogContentImage(MultipartFile file) {
        try {
            return this.baseUploadImage( BLOG_IMAGE_PATH , file );
        } catch (IOException e) {
            log.error( "文件解析错误" );
            throw new CommonException( "文件解析错误" );
        }
    }

    /**
     * 获取博客内容图片
     * @return
     */
    @Override
    public URL getBlogContentImage( @NonNull String objectName , @Nullable Long expire ) {
        return this.baseGetImage( objectName , expire );
    }


}
